//
// Created by yanhai on 2020/2/28.
//

#include <iostream>
#include <vector>
#include <utility/os.hpp>
#include <utility/crypto/md5/md5.h>
#include <utility/encoding/base64/base64.h>

using namespace std;
using namespace muh::os::path;

void test1()
{
    cout << isdir("/tmp/test1") << endl;
    cout << muh::os::path::isfile("/tmp/test2") << endl;
}

void test2()
{
    vector<string> v;
    v.emplace_back("/var/log/test1");
    v.emplace_back("/var/log/test2");
    v.emplace_back("/var/log/ftp");
    cout << muh::os::path::commonprefix(v.begin(), v.end()) << endl;
}

template<typename T>
void printByteToHexString(T &t)
{
    for (const auto &c: t) {
        printf("%02x", c);
    }
    printf("\n");
}

void test_md5()
{
    // e2c569be17396eca2a2e3c11578123ed
    std::array<unsigned char, 16> expected = {
            0xe2, 0xc5, 0x69, 0xbe,
            0x17, 0x39, 0x6e, 0xca,
            0x2a, 0x2e, 0x3c, 0x11,
            0x57, 0x81, 0x23, 0xed,
    };

    muh::crypto::Md5 md5;
    md5.addData("The fog is getting thicker!");
    md5.addData("And Leon's getting laaarger!");
    auto sum = md5.sum();
    printByteToHexString(sum);

    assert(sum == expected);
    assert(sum == muh::crypto::Md5::sum("The fog is getting thicker!And Leon's getting laaarger!"));
}

void test_base64()
{
    std::string src("The fog is getting thicker!And Leon's getting laaarger!");
    std::string expected("VGhlIGZvZyBpcyBnZXR0aW5nIHRoaWNrZXIhQW5kIExlb24ncyBnZXR0aW5nIGxhYWFyZ2VyIQ==");
    std::string expectedOmitTrailingEquals(
            "VGhlIGZvZyBpcyBnZXR0aW5nIHRoaWNrZXIhQW5kIExlb24ncyBnZXR0aW5nIGxhYWFyZ2VyIQ");

    // encode
    auto s = muh::encoding::Base64::encodeToString(src);
    std::cout << s << std::endl;    // VGhlIGZvZyBpcyBnZXR0aW5nIHRoaWNrZXIhQW5kIExlb24ncyBnZXR0aW5nIGxhYWFyZ2VyIQ==
    assert(s == expected);

    auto s2 = muh::encoding::Base64::encodeToString(src, muh::encoding::Base64::OmitTrailingEquals);
    std::cout << s2 << std::endl;    // VGhlIGZvZyBpcyBnZXR0aW5nIHRoaWNrZXIhQW5kIExlb24ncyBnZXR0aW5nIGxhYWFyZ2VyIQ
    assert(s2 == expectedOmitTrailingEquals);

    // decode
    auto s3 = muh::encoding::Base64::fromBase64(s);
    string s3s(s3.begin(), s3.end());
    std::cout << s3s << std::endl;    // The fog is getting thicker!And Leon's getting laaarger!
    assert(s3s == src);

    auto s4 = muh::encoding::Base64::fromBase64(s2);
    string s4s(s4.begin(), s4.end());
    std::cout << s4s << std::endl;    // The fog is getting thicker!And Leon's getting laaarger!
    assert(s4s == src);
}

int main()
{
    test1();
    test2();
    test_md5();
    test_base64();

    return 0;
}
